import {_decorator, Label} from "cc";
import {serverTimeHelper} from "db://assets/script/server-time-helper";

const {ccclass, property, menu} = _decorator;

@ccclass("LabelTime")
@menu('ui/label/LabelTime')
export default class LabelTime extends Label {
    @property({
        tooltip: "到计时间总时间（单位秒）"
    })
    countDown: number = 1000;

    prefixText: string = '';

    @property({
        tooltip: "天数数据格式化"
    })
    dayFormat: string = "{0} day";

    @property({
        tooltip: "时间格式化"
    })
    timeFormat: string = "{0}:{1}:{2}";

    @property({
        tooltip: "是否有00"
    })
    zeroize: boolean = true;

    private dateDisable!: boolean;
    private result!: string;

    /** 每秒触发事件 */
    onSecond: Function = null!;
    /** 倒计时完成事件 */
    onComplete: Function | null = null!;

    endServerTime = 0;

    private replace(value: string, ...args: any): string {
        return value.replace(/\{(\d+)\}/g,
            function (m, i) {
                return args[i];
            });
    }

    /** 格式化字符串 */
    private format() {
        let c: number = this.countDown;
        let date: number = Math.floor(c / 86400);
        c = c - date * 86400;
        let hours: number = Math.floor(c / 3600);
        c = c - hours * 3600;
        let minutes: number = Math.floor(c / 60);
        c = c - minutes * 60;
        let seconds: number = c;

        this.dateDisable = this.dateDisable || false;
        if (date == 0 && hours == 0 && minutes == 0 && seconds == 0) {
            if (this.zeroize) {
                this.result = this.replace(this.timeFormat, "00", "00", "00");
            } else {
                this.result = this.replace(this.timeFormat, "0", "0", "0");
            }
        } else if (date > 0 && !this.dateDisable) {
            let dataFormat = this.dayFormat;
            let df = dataFormat;
            if (date > 1 && dataFormat.indexOf("days") < 0) {
                df = df.replace("day", "days");
            }
            if (date < 2) {
                df = df.replace("days", "day");
            }
            this.result = this.replace(df, date, hours, minutes);                      // 如果天大于1，则显示 "1 Day..."
        } else {
            hours += date * 24;
            if (this.zeroize) {
                this.result = this.replace(
                    this.timeFormat,
                    this.coverString(hours),
                    this.coverString(minutes),
                    this.coverString(seconds));                                            // 否则显示 "01:12:24"
            } else {
                this.result = this.replace(
                    this.timeFormat,
                    hours,
                    minutes,
                    seconds);
            }
        }
        this.string = this.prefixText + this.result;
    }

    /** 个位数的时间数据将字符串补位 */
    private coverString(value: number) {
        if (value < 10)
            return "0" + value;
        return value.toString();
    }

    /** 重置 */
    reset() {
        this.countDown = 0;
        clearInterval(this.timeoutid);
    }

    /** 设置时间能否由天数显示 */
    setDateDisable(flag: boolean) {
        this.dateDisable = flag;
    }

    /** 设置倒计时时间 */
    setTime(second: number, isStart = true) {
        second = second > 0 ? second : 0;
        this.countDown = second;                                             // 倒计时，初始化显示字符串
        if (isStart) {
            this.timing_end();
            this.timing_start();
        } else {
            this.format()
        }
    }

    /**根据服务器结束时间设置倒计时 */
    setTimeByServerTime(endTime: number, isStart = true) {
        this.endServerTime = endTime;
        let stMs = Math.floor(serverTimeHelper.loadServerTime());
        this.countDown = Math.floor((this.endServerTime - stMs) / 1000);
        this.countDown = Math.max(this.countDown, 0)
        if (isStart) {
            this.timing_end();
            this.timing_start(1000);
        } else {
            this.format()
        }
    }

    setTimeFormat(str: string) {
        this.timeFormat = str;
        this.dayFormat = str;
    }

    private onScheduleSecond() {
        if (!this.endServerTime) {
            this.countDown--;
        } else {
            let stMs = Math.floor(serverTimeHelper.loadServerTime());
            this.countDown = Math.floor((this.endServerTime - stMs) / 1000);
        }
        this.countDown = Math.max(this.countDown, 0)
        this.format();
        if (this.onSecond) this.onSecond(this.node);

        if (this.countDown <= 0) {
            this.onScheduleComplete();
        }
    }

    private onScheduleComplete() {
        this.timing_end();
        if (this.onComplete) {
            this.onComplete(this.node);
            this.onComplete = null;
        }

    }

    timeoutid: any = null;

    /** 开始计时 */
    private timing_start(ms = 1000) {

        this.format();
        if (this.countDown <= 0) {
            this.onScheduleComplete();
            return;
        }
        this.timeoutid = setInterval(() => {
            if (!this.isValid) {
                clearInterval(this.timeoutid)
                return
            }
            this.onScheduleSecond();
        }, ms)
    }

    private timing_end() {
        // this.unscheduleAllCallbacks();
        clearInterval(this.timeoutid)
        this.format();
    }
}
